using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCT.Framework;
using System.Data;
using System.Data.Common;
using NCT.Framework.Database;

namespace VISS.API.Users {
	class UserFamilyProfileDA {

		private const string SP_GetAll = "dbo.UserFamilyProfile_GetAll";
		private const string SP_GetByUserKey = "dbo.UserFamilyProfile_GetByUserKey";
		private const string SP_Insert = "dbo.UserFamilyProfile_Insert";
		private const string SP_Update = "dbo.UserFamilyProfile_Update";

		public static UserFamilyProfileEnt CreateFromReader(IDataReader reader) {
			UserFamilyProfileEnt item = new UserFamilyProfileEnt();
			item.UserKey = ConvertUtils.ToString(reader["UserKey"]);
			item.MaritalStatusID = ConvertUtils.ToShort(reader["MaritalStatusID"]);
			item.MaritalStatus = ConvertUtils.ToString(reader["MaritalStatus"]);
			item.HasChild = ConvertUtils.ToBool(reader["HasChild"]);
			item.WantChild = ConvertUtils.ToBool(reader["WantChild"]);
			item.DateCreated = ConvertUtils.ToDateTime(reader["DateCreated"]);
			item.DateModified = ConvertUtils.ToDateTime(reader["DateModified"]);
			return item;
		}

        public static UserFamilyProfileEnt GetAll(string userKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			UserFamilyProfileEnt result = null;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_GetAll)) {
				db.AddParameter(cmd, "UserKey", userKey);
				using (IDataReader reader = db.ExecuteReader(cmd)) {
					if (reader.Read()) {
						result = CreateFromReader(reader);
					}
				}
			}
			return result;
		}

        public static UserFamilyProfileEnt GetByUserKey(string userKey, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			UserFamilyProfileEnt result = null;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_GetByUserKey)) {
				db.AddParameter(cmd, "UserKey", userKey);
				using (IDataReader reader = db.ExecuteReader(cmd)) {
					if (reader.Read()) {
						result = CreateFromReader(reader);
					}
				}
			}
			return result;
		}

		public static int Insert(UserFamilyProfileEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Insert)) {
				db.AddParameter(cmd, "UserKey", item.UserKey);
				db.AddParameter(cmd, "MaritalStatusID", item.MaritalStatusID);
				db.AddParameter(cmd, "HasChild", item.HasChild);
				db.AddParameter(cmd, "WantChild", item.WantChild);
				db.AddParameter(cmd, "DateCreated", item.DateCreated);
				db.AddParameter(cmd, "DateModified", item.DateModified);

				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

        public static int Update(UserFamilyProfileEnt item, string connectionName, DbConnection dbCon, DbTransaction dbTrans) {
			int result = -1;
			Database db = Database.CreateDatabase(connectionName);
			using (DbCommand cmd = db.CreateStoredCommand(SP_Update)) {
				db.AddParameter(cmd, "UserKey", item.UserKey);
				db.AddParameter(cmd, "MaritalStatusID", item.MaritalStatusID);
				db.AddParameter(cmd, "HasChild", item.HasChild);
				db.AddParameter(cmd, "WantChild", item.WantChild);
				db.AddParameter(cmd, "DateCreated", item.DateCreated);
				db.AddParameter(cmd, "DateModified", item.DateModified);
				if (dbCon != null && dbTrans != null) {
					result = db.ExecuteNonQuery(cmd, dbCon, dbTrans);
				}
				else {
					result = db.ExecuteNonQuery(cmd);
				}
			}
			return result;
		}

	}
}